package by.twouexplorer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileWorker {
	
	public void rename(File oldFile, File newFile){
		oldFile.renameTo(newFile);		//renaming oldFile to newFile
	}
	
	public void move(File oldPath, File newPath){
		oldPath.renameTo(newPath); //moving oldPath file/folder to newPath
	}
	
	public void createFile(String dir,String name) {
		
		File F = new File(dir+"/"+name);
		try {
			F.createNewFile();  //creating file "name" in directory "dir"
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	 public boolean createDir(String path, String name) {
         int len = path.length();
         if(len < 1)
        	 return false;
         if(path.charAt(len - 1) != '/')
             path += "/";
         if (new File(path+name).mkdir()){
        	 return true;        	 
         }
         else return false;
     }
	 
	 public boolean deleteTarget(String path) {
			
         File target = new File(path);
         
         if(target.exists() && target.isFile() && target.canWrite()) {
                 target.delete();
                 return true;
         }
         
         else if(target.exists() && target.isDirectory() && target.canRead()) {
                 String[] file_list = target.list();
                 
                 if(file_list != null && file_list.length == 0) {
                         target.delete();
                         return true;
                         
                 } else if(file_list != null && file_list.length > 0) {
                         
                         for(int i = 0; i < file_list.length; i++) {
                                 File temp_f = new File(target.getAbsolutePath() + "/" + file_list[i]);




                                 if(temp_f.isDirectory())
                                         deleteTarget(temp_f.getAbsolutePath());
                                 else if(temp_f.isFile())
                                         temp_f.delete();
                         }
                 }
                 if(target.exists())
                         if(target.delete()){
                        	 return true;
                         }
         }       
         return false;
	 }
}
